<?php
namespace Admin\Controller;
use Admin\Model\ConfigModel;

/**
 * 给业务员看的产品
 */
class ProduceController extends BaseController
{
    /**
     * 获取所有产品列表
     */
    public function index()
    {
        $this->assign('search', $this->data);
        $where = array(
            'is_delete' => 0
        );
        if($this->data['title']){
            $where['name'] = array('like','%'.$this->data['name'].'%');
        }
        $count = M('produce')->where($where)->count();
        $Page = new \Think\Page($count,$this->perpage);
        $list =  M('produce')->where($where)->Page($this->p,$this->perpage)->order($this->desc)->select();
        $this->assign('list', $list);
        $this->assign('page', $Page->show());
        $this->assign('list_table', true);
        $this->display();
    }

    /**
     * 获取业务员可查看产品列表
     */
    public function listOf()
    {
        $this->assign('search', $this->data);
        $where = array(
            'status' => 1,
            'is_delete' => 0
        );
        if($this->data['title']){
            $where['name'] = array('like','%'.$this->data['name'].'%');
        }
        $count = M('produce')->where($where)->count();
        $Page = new \Think\Page($count,$this->perpage);
        $list =  M('produce')->where($where)->Page($this->p,$this->perpage)->order($this->desc)->select();
        $this->assign('list', $list);
        $this->assign('page', $Page->show());
        $this->assign('list_table', true);
        $this->display('list');
    }

    /**
     * 添加或编辑操作
     */
    public function add(){
        if(IS_POST){
            $post=I('post.');
            if(!$post['name']){
                $this->error('请填写产品名称');
            }
            if(!$post['scope_start']){
                $this->error('请填写机构名称');
            }
            if(!$post['pic']){
                $post['pic']='/static/images/nopic.jpg';
            }
            if(!$post['create_time']){
                $post['create_time'] = strtotime($post['create_time']);
            } else{
                $post['create_time'] = time();
            }
            if($post['id']){
                M('produce')->where(array('id'=>$post['id']))->save($post)!==false?$this->success('修改成功',U('produce/index')):$this->error('修改失败');
            }else{
                M('produce')->add($post)?$this->success('添加成功',U('product/index')):$this->error('添加失败');
            }
        }else{
            if(I('get.id')){
                $this->assign('info',M('produce')->where(array('id'=>I('get.id')))->find());
            }
            $this->assign('product_type',(new ConfigModel())->getConfigByNameModel('product_type'));
            $this->display();
        }
    }

    /**
     * 删除操作
     */
    public function delete(){
        $ids = array_filter(array_unique(explode(',',I('get.id'))));
        if(!$ids){
            $this->errorjsonReturn('请选择需删除项');
        }
        $result = M('produce')->where(array('id'=>array('in',$ids)))->save(array('is_delete'=>1));
        $result===false?$this->errorjsonReturn('删除失败'):$this->setjsonReturn('删除成功');
    }

    /**
     * 修改排序
     */
    public function ajax_edit()
    {
        $get = I('get.');
        if(!$get['id']||!$get['field']){
            $this->errorjsonReturn('修改失败');
        }
        $result = M('produce')->where(array('id'=>$get['id']))->save(array($get['field']=>$get['val']));
        $result===false?$this->errorjsonReturn('修改失败'):$this->setjsonReturn('修改成功');
    }
}